package newFeature;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author LiuYan
 * @date 2023/4/20
 * 集合类新增工厂方法
 */
public class CollectionOptimization {
    public static void main(String[] args) {
        // Java8使用Map，需要先创建一个Map对象，然后再添加数据
        HashMap<String, Integer> oldMap = new HashMap<>();
        oldMap.put("k1", 19);
        oldMap.put("k2", 23);
        System.out.println(oldMap);
        // Java9可以通过of方法来快速创建
        Map<String, Integer> newMap = Map.of("k1", 18, "k2", 20);//直接搞定
        System.out.println(newMap);
        List<String> list = List.of("value1", "value2", "value3");
        System.out.println(list);
        Set<String> set = Set.of("value1", "value2", "value3");
        // set.add("value4");  Exception in thread "main" java.lang.UnsupportedOperationException
        System.out.println(set);
        Map.Entry entry1 = Map.entry("k1", "v1");
        Map.Entry entry2 = Map.entry("k2", "v2");
        Map.Entry entry3 = Map.entry("k3", "v3");
        Map mapOfEntries = Map.ofEntries(entry1, entry2, entry3);
        System.out.println(mapOfEntries);
    }
}
